package com.yuou.controller.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuou.annotation.AccessLimit;
import com.yuou.annotation.SystemLog;
import com.yuou.controller.BaseExceptionController;
import com.yuou.mapper.RoleMenuMapper;
import com.yuou.model.Role;
import com.yuou.model.RoleMenu;
import com.yuou.service.RoleService;
import com.yuou.util.ConstantUtil;

@Controller
@RequestMapping(value = "/admin")
public class RoleControllerAdmin extends BaseExceptionController {
	@Resource(name = "roleServiceImpl")
	private RoleService roleService;
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	
	/**
	 * 模糊组合分页查询角色信息(and)
	 * @param bloger
	 * @param page
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	 @RequiresPermissions("sys:role:role")
	 @RequestMapping(value = "/selectAllRoles")
	 @ResponseBody
	 @AccessLimit(seconds=1,maxCount=15)
	 public Map<String, Object> selectAllRoles(Role role,@RequestParam(value="page", required=true,defaultValue="1") Integer page,@RequestParam(value="pageSize", required=true,defaultValue="10") Integer pageSize) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(role.getRoleName()!=null&&role.getRoleName()!=""){
			 map.put("roleName", role.getRoleName());
		 }
		 if(role.getRoleSign()!=null&&role.getRoleSign()!=""){
			 map.put("roleSign", role.getRoleSign());
		 }
			 map.put("status", role.getStatus());
		 //分页显示：第1页开始，每页显示10条记录
		 PageHelper.startPage(page, pageSize);
		 List<Role> roleList=roleService.selectAll(map);
		 PageInfo<Role> pageInfo=new PageInfo<Role>(roleList);
		 Map<String, Object> returnMap=new HashMap<String, Object>();
		 if(roleList.size()>0){
			 returnMap.put("status", 200);
		 }else{
			 //500表示：返回值为Null
			 returnMap.put("status", 500);
		 }
		 
		 returnMap.put("roleList", roleList);
		 returnMap.put("pageInfo", pageInfo);
		 return returnMap;
	 }
	 /**
	  * 查询角色信息(and)
	  * @return
	  * @throws Exception
	  */
	 @RequestMapping(value = "/selectAll")
	 @ResponseBody
	 public Map<String, Object> selectAll() throws Exception{
		 List<Role> roleList=roleService.selectAll(null);
		 Map<String, Object> returnMap=new HashMap<String, Object>();
		 if(roleList.size()>0){
			 returnMap.put("status", 200);
		 }else{
			 //500表示：返回值为Null
			 System.out.println("RoleController为空啦============");
			 returnMap.put("status", 500);
		 }
		 
		 returnMap.put("roleList", roleList);
		 return returnMap;
	 }
	 
	 /**
	  * 按用户角色id查询用户角色信息
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:role:role")
	 @RequestMapping(value = "/selectRoleById",method = RequestMethod.POST)
	 @ResponseBody
	 public Map<String, Object> selectRoleById(Integer id) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 Role role=roleService.selectByPrimaryKey(id);
		 
		 if(role!=null){
			 map.put("status", 200);
		 }else{
			 //500表示：返回值为Null
			 map.put("status", 500);
		 }
		 map.put("role", role);
		 return map;
	 }
	 /**
	  * 更新角色功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:role:edit")
	 @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.ROLE_MODIFY,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> updateRole(String prarm,Role role) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 try {
		 if(roleService.updateByPrimaryKey(role)!=0){
			 List<Integer> menuIds = role.getMenuIds();
			 	Integer roleId = role.getId();
		        roleMenuMapper.removeByRoleId(roleId);
		        List<RoleMenu> rms = new ArrayList<>();
		        if(menuIds!=null) {
			        for (Integer menuId : menuIds) {
			            RoleMenu rmDo = new RoleMenu();
			            rmDo.setRoleId(roleId);
			            rmDo.setMenuId(menuId);
			            rms.add(rmDo);
			        }
		        }
		        if (rms.size() > 0) {
		        	roleMenuMapper.batchSave(rms);
		        }
		        map.put("status", 200);
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 }catch(Exception e) {
			 e.printStackTrace();
		 }
		 return map;
	 }
	 /**
	  * 添加角色功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:role:add")
	 @RequestMapping(value = "/addRole",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.ROLE_ADD,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> addRole(String prarm,Role role) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 role.setGmtCreate(new Date());
		 if(roleService.insertRole(role)!=0){
			 List<Integer> menuIds = role.getMenuIds();
			 //System.out.println("===========menuIds"+menuIds);
			 //System.out.println("===========menuIds"+menuIds.toString());
			 	Integer roleId = role.getId();
		        roleMenuMapper.removeByRoleId(roleId);
		        List<RoleMenu> rms = new ArrayList<>();
		        for (Integer menuId : menuIds) {
		            RoleMenu rmDo = new RoleMenu();
		            rmDo.setRoleId(roleId);
		            rmDo.setMenuId(menuId);
		            rms.add(rmDo);
		        }
		        if (rms.size() > 0) {
		        	roleMenuMapper.batchSave(rms);
		            	map.put("status", 200);
		        }
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 return map;
	 }
	 
	 /**
	  * 添加角色功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:role:remove")
	 @RequestMapping(value = "/delRole",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.ROLE_DELETE,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> delRole(String prarm,Integer id) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(id!=0){
		        roleMenuMapper.removeByRoleId(id);
		        if (roleService.deleteByPrimaryKey(id) > 0) {
		            	map.put("status", 200);
		        }
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 return map;
	 }
}
